import axios from 'axios';

const API_URL = process.env.NEXT_PUBLIC_API_URL;

export interface PhotoSize {
  width: number;
  height: number;
}

export interface PhotoResult {
  preview: string;
  layout: string;
  transparentBase64: string;  // 新增：存储透明背景的base64
  originalPreview: string;
  originalLayout: string;
}

export const generateTransparentPhoto = async (file: File, size: PhotoSize): Promise<string> => {
  const formData = new FormData();
  formData.append('input_image', file);
  formData.append('width', size.width.toString());
  formData.append('height', size.height.toString());

  try {
    const idPhotoResponse = await axios.post(`${API_URL}/idphoto`, formData);
    return idPhotoResponse.data.image_base64_standard;
  } catch (error) {
    throw error;
  }
};

export const generatePhoto = async (file: File, size: PhotoSize, backgroundColor: string): Promise<PhotoResult> => {
  const formData = new FormData();
  formData.append('input_image', file);
  formData.append('width', size.width.toString());
  formData.append('height', size.height.toString());

  try {
    // 生成证件照
    const idPhotoResponse = await axios.post(`${API_URL}/idphoto`, formData);
    
    const transparentBase64 = idPhotoResponse.data.image_base64_standard;
    
    // 添加背景色
    const backgroundFormData = new FormData();
    backgroundFormData.append('input_image_base64', transparentBase64);
    backgroundFormData.append('color', backgroundColor.substring(1));
    const backgroundResponse = await axios.post(`${API_URL}/add_background`, backgroundFormData);
    
    // 生成六寸排版照
    const layoutFormData = new FormData();
    layoutFormData.append('input_image_base64', backgroundResponse.data.image_base64);
    layoutFormData.append('width', size.width.toString());
    layoutFormData.append('height', size.height.toString());
    const layoutResponse = await axios.post(`${API_URL}/generate_layout_photos`, layoutFormData);

    // 添加水印
    const previewWithWatermark = await addWatermark(backgroundResponse.data.image_base64);
    const layoutWithWatermark = await addWatermark(layoutResponse.data.image_base64);

    return {
      preview: previewWithWatermark,
      layout: layoutWithWatermark,
      transparentBase64: transparentBase64,
      originalPreview: backgroundResponse.data.image_base64,
      originalLayout: layoutResponse.data.image_base64
    };
  } catch (error) {
    throw error;
  }
};

export const makePayment = async () => {
  try {
    const response = await axios.post('/api/payment');
    return response.data.success;
  } catch (error) {
    throw error;
  }
};

export const changeBackgroundColor = async (transparentBase64: string, backgroundColor: string): Promise<string> => {
  try {
    const backgroundFormData = new FormData();
    backgroundFormData.append('input_image_base64', transparentBase64);
    backgroundFormData.append('color', backgroundColor.substring(1));
    const backgroundResponse = await axios.post(`${API_URL}/add_background`, backgroundFormData);
    
    if (backgroundResponse.data && backgroundResponse.data.image_base64) {
      return backgroundResponse.data.image_base64;
    } else {
      throw new Error('Invalid response from add_background API');
    }
  } catch (error) {
    throw error;
  }
};

export const addWatermark = async (imageBase64: string): Promise<string> => {
  try {
    const watermarkFormData = new FormData();
    watermarkFormData.append('input_image_base64', imageBase64);
    watermarkFormData.append('text', '证件魔术师');
    const watermarkResponse = await axios.post(`${API_URL}/watermark?size=20&opacity=0.5&angle=30&color=%23FFFFFF&space=25`, watermarkFormData);
    
    if (watermarkResponse.data && watermarkResponse.data.image_base64) {
      return watermarkResponse.data.image_base64;
    } else {
      throw new Error('Invalid response from watermark API');
    }
  } catch (error) {
    throw error;
  }
};

export const generateLayout = async (imageBase64: string, size: PhotoSize): Promise<string> => {
  try {
    const layoutFormData = new FormData();
    layoutFormData.append('input_image_base64', imageBase64);
    layoutFormData.append('width', size.width.toString());
    layoutFormData.append('height', size.height.toString());
    const layoutResponse = await axios.post(`${API_URL}/generate_layout_photos`, layoutFormData);

    if (layoutResponse.data && layoutResponse.data.image_base64) {
      return layoutResponse.data.image_base64;
    } else {
      throw new Error('Invalid response from generate_layout_photos API');
    }
  } catch (error) {
    throw error;
  }
};
